package com.elangzhi.ssm.modules.receiptAddress.app;

import com.elangzhi.ssm.controller.json.Tip;
import com.elangzhi.ssm.model.ReceiptAddress;
import com.elangzhi.ssm.modules.receiptAddress.services.ReceiptAddressService;
import com.elangzhi.ssm.tools.UUIDFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
* 收货地址 客户端 Controller
* @author GaoXiang
* @version 1.0
*/
@Controller
@RequestMapping("/app/receiptAddress")
public class AppReceiptAddressController {

    /**
     * 添加用户收货地址
     * @param receiptAddress 参数实体
     * @return 成功状态
     */
    @RequestMapping("/save")
    @ResponseBody
    public Tip save(ReceiptAddress receiptAddress){

        try {
            receiptAddress.setId(UUIDFactory.getLongId());
            receiptAddressService.insert(receiptAddress);
            return new Tip();
        } catch (Exception e) {
            e.printStackTrace();
            return new Tip(1);
        }
    }

    /**
     * 根据用户id获取用户收货地址列表
     * @param userId 用户id
     * @return 用户收货地址列表
     */
    @RequestMapping("/listByUserId")
    @ResponseBody
    public List<ReceiptAddress> listByUserId(Long userId){
        return receiptAddressService.listByUserId(userId);
    }

    /**
     * 删除用户收货地址
     * @param id 实体id
     * @return 成功状态
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Tip delete(Long id){
        try {
            receiptAddressService.deleteById(id);
            return new Tip();
        } catch (Exception e) {
            e.printStackTrace();
            return new Tip(1);
        }
    }

    /**
     * 更改用户收货地址
     * @param receiptAddress 实体
     * @return 成功状态
     */
    @RequestMapping("/update")
    @ResponseBody
    public Tip update(ReceiptAddress receiptAddress){
        try {
            receiptAddressService.updateById(receiptAddress);
            return new Tip();
        } catch (Exception e) {
            e.printStackTrace();
            return new Tip(1);
        }
    }


    //---------------------------- property -------------------------------

    @Resource
    private ReceiptAddressService receiptAddressService;

}
